package com.ss.android.common.imagezoom;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;

import com.bytedance.common.utility.Logger;
import com.ss.android.common.imagezoom.easing.Cubic;
import com.ss.android.common.imagezoom.graphics.FastBitmapDrawable;

import inline.ScreenshotListener;

public class ImageViewTouchBase extends ImageView {
    private Matrix a;
    private Runnable b;
    private float c;
    private float d;
    private Matrix e;
    private float[] f;
    private int g;
    private int h;
    protected Matrix i;
    protected Handler j;
    protected boolean k;
    protected RectF l;
    protected boolean m;
    private boolean n;
    private RectF o;
    private RectF p;
    private float[] q;

    public ImageViewTouchBase(Context arg4) {
        super(arg4);
        new Cubic();
        this.a = new Matrix();
        this.i = new Matrix();
        this.j = new Handler();
        this.b = null;
        this.d = -1f;
        this.e = new Matrix();
        this.f = new float[9];
        this.g = -1;
        this.h = -1;
        this.k = false;
        this.n = false;
        this.o = new RectF();
        this.p = new RectF();
        this.l = new RectF();
        this.q = new float[1];
        this.a();
    }

    public ImageViewTouchBase(Context arg4, AttributeSet arg5) {
        super(arg4, arg5);
        new Cubic();
        this.a = new Matrix();
        this.i = new Matrix();
        this.j = new Handler();
        this.b = null;
        this.d = -1f;
        this.e = new Matrix();
        this.f = new float[9];
        this.g = -1;
        this.h = -1;
        this.k = false;
        this.n = false;
        this.o = new RectF();
        this.p = new RectF();
        this.l = new RectF();
        this.q = new float[1];
        this.a();
    }

    protected void a() {
        this.setScaleType(ImageView.ScaleType.MATRIX);
    }

    private void a(float arg2, float arg3) {
        this.i.postTranslate(arg2, arg3);
        this.setImageMatrix(this.getImageViewMatrix());
    }

    private void a(Drawable arg10, Matrix arg11) {
        float v8 = 2f;
        Log.i("image", "getProperBaseMatrix");
        float v0 = ((float)this.getWidth());
        float v1 = ((float)this.getHeight());
        float v2 = ((float)arg10.getIntrinsicWidth());
        float v3 = ((float)arg10.getIntrinsicHeight());
        arg11.reset();
        if(v2 > v0 || v3 > v1) {
            float v4 = Math.min(Math.min(v0 / v2, v8), Math.min(v1 / v3, v8));
            Log.d("image", "scale: " + v4);
            arg11.postScale(v4, v4);
            arg11.postTranslate((v0 - v2 * v4) / v8, (v1 - v3 * v4) / v8);
        }
        else {
            arg11.postTranslate((v0 - v2) / v8, (v1 - v3) / v8);
            Log.d("image", "scale: null");
        }
    }

    private void a(Drawable arg10, Matrix arg11, float[] arg12) {
        float v7 = 1f;
        float v2 = ((float)this.getWidth());
        float v3 = ((float)arg10.getIntrinsicWidth());
        arg11.reset();
        float v0 = v3 == 0f ? 0f : v2 / v3;
        v0 = v0 < 10f ? Math.min(v0, 5f) : Math.min(v0, 30f);
        Logger.d("ImageViewTouchBase", "get scale3 " + v0 + " " + v3 + " " + v2);
        arg11.postScale(v0, v0);
        arg11.postTranslate((v2 - v3 * v0) / 2f, 0f);
        arg12[0] = v0 > v7 ? v7 / v0 : v7;
    }

    protected final RectF a(Matrix arg8) {
        float v2_1;
        RectF v0;
        float v6 = 2f;
        if(this.getDrawable() == null) {
            v0 = new RectF(0f, 0f, 0f, 0f);
        }
        else {
            this.p.set(0f, 0f, 0f, 0f);
            RectF v2 = this.c(arg8);
            float v0_1 = v2.height();
            float v3 = v2.width();
            int v4 = this.getHeight();
            if(v0_1 < (((float)v4))) {
                v0_1 = ((((float)v4)) - v0_1) / v6 - v2.top;
            }
            else if(v2.top > 0f) {
                v0_1 = -v2.top;
            }
            else if(v2.bottom < (((float)v4))) {
                v0_1 = (((float)this.getHeight())) - v2.bottom;
            }
            else {
                v0_1 = 0f;
            }

            v4 = this.getWidth();
            if(v3 < (((float)v4))) {
                v2_1 = ((((float)v4)) - v3) / v6 - v2.left;
            }
            else if(v2.left > 0f) {
                v2_1 = -v2.left;
            }
            else if(v2.right < (((float)v4))) {
                v2_1 = (((float)v4)) - v2.right;
            }
            else {
                v2_1 = 0f;
            }

            this.p.set(v2_1, v0_1, 0f, 0f);
            v0 = this.p;
        }

        return v0;
    }

    protected final void a(double arg6, double arg8) {
        RectF v0 = this.getBitmapRect();
        this.l.set(((float)arg6), ((float)arg8), 0f, 0f);
        this.a(v0, this.l);
        this.a(this.l.left, this.l.top);
        this.c();
    }

    protected final void a(RectF arg6, RectF arg7) {
        float v0 = ((float)this.getWidth());
        float v1 = ((float)this.getHeight());
        if(arg6.top >= 0f && arg6.bottom <= v1) {
            arg7.top = 0f;
        }

        if(arg6.left >= 0f && arg6.right <= v0) {
            arg7.left = 0f;
        }

        if(arg6.top + arg7.top >= 0f && arg6.bottom > v1) {
            arg7.top = ((float)(((int)(0f - arg6.top))));
        }

        if(arg6.bottom + arg7.top <= v1 && arg6.top < 0f) {
            arg7.top = ((float)(((int)(v1 - arg6.bottom))));
        }

        if(arg6.left + arg7.left >= 0f) {
            arg7.left = ((float)(((int)(0f - arg6.left))));
        }

        if(arg6.right + arg7.left <= v0) {
            arg7.left = ((float)(((int)(v0 - arg6.right))));
        }
    }

    protected void a(float arg1) {
    }

    protected final void a(float arg5, float arg6, float arg7) {
        if(arg5 > this.c) {
            arg5 = this.c;
        }

        float v0 = arg5 / this.getScale();
        Logger.d("image", "zoomTo: " + arg5 + ", center: " + arg6 + "x" + arg7);
        this.i.postScale(v0, v0, arg6, arg7);
        this.setImageMatrix(this.getImageViewMatrix());
        this.a(this.getScale());
        this.c();
    }

    protected final void a(float arg11, float arg12, float arg13, float arg14) {
        if(arg11 > this.getMaxZoom()) {
            arg11 = this.getMaxZoom();
        }

        long v4 = System.currentTimeMillis();
        float v7 = this.getScale();
        Matrix v0 = new Matrix(this.i);
        v0.postScale(arg11, arg11, arg12, arg13);
        RectF v0_1 = this.a(v0);
        this.j.post(new com.ss.android.common.imagezoom.d(this, arg14, v4, arg11 - v7, v7, arg12 + v0_1.left * arg11, arg13 + v0_1.top * arg11));
    }

    protected void a(Drawable arg1) {
    }

    protected void a(Drawable arg3, boolean arg4, Matrix arg5, float arg6) {
        if(arg3 != null) {
            if(this.k) {
                float[] v0 = new float[1];
                this.a(arg3, this.a, v0);
                this.setMinZoom(v0[0]);
            }
            else if(this.n) {
                this.b(arg3, this.a);
                this.setMinZoom(this.d(this.a));
            }
            else {
                this.a(arg3, this.a);
                this.setMinZoom(this.getMinZoom());
            }

            super.setImageDrawable(arg3);
        }
        else {
            this.a.reset();
            super.setImageDrawable(null);
        }

        if(arg4) {
            this.i.reset();
            if(arg5 != null) {
                this.i = new Matrix(arg5);
            }
        }

        this.setImageMatrix(this.getImageViewMatrix());
        this.c = arg6 < 1f ? this.b() : arg6;
        this.a(arg3);
    }

    private float b() {
        float v0 = 1f;
        Drawable v1 = this.getDrawable();
        if(v1 != null) {
            float v1_1 = Math.max((((float)v1.getIntrinsicWidth())) / (((float)this.g)), (((float)v1.getIntrinsicHeight())) / (((float)this.h))) * 4f;
            if(v1_1 >= v0) {
                v0 = v1_1;
            }
        }

        return v0;
    }

    private Matrix b(Matrix arg3) {
        this.e.set(this.a);
        this.e.postConcat(arg3);
        return this.e;
    }

    private void b(Drawable arg10, Matrix arg11) {
        float v1;
        float v8 = 30f;
        float v7 = 5f;
        float v6 = 2f;
        float v0 = 0f;
        float v2 = ((float)this.getWidth());
        float v3 = ((float)this.getHeight());
        float v4 = ((float)arg10.getIntrinsicWidth());
        float v5 = ((float)arg10.getIntrinsicHeight());
        arg11.reset();
        if(v4 != 0f) {
            v0 = v2 / v4;
        }

        if(v0 < 10f) {
            v1 = Math.min(v2 / v4, v7);
            v0 = Math.min(v3 / v5, v7);
        }
        else {
            v1 = Math.min(v0, v8);
            v0 = Math.min(v3 / v5, v8);
        }

        v0 = Math.min(v1, v0);
        arg11.postScale(v0, v0);
        arg11.postTranslate((v2 - v4 * v0) / v6, (v3 - v0 * v5) / v6);
    }

    protected void b(float arg1) {
    }

    public final void b(float arg4, float arg5) {
        this.a(arg4, (((float)this.getWidth())) / 2f, (((float)this.getHeight())) / 2f, arg5);
    }

    public final void b(Drawable arg7, boolean arg8, Matrix arg9, float arg10) {
        if(this.getWidth() <= 0) {
            this.b = new bb(this, arg7, arg8, arg9, arg10);
        }
        else {
            this.a(arg7, arg8, arg9, arg10);
        }
    }

    private RectF c(Matrix arg6) {
        Drawable v0 = this.getDrawable();
        Matrix v1 = this.b(arg6);
        if(v0 != null) {
            this.o.set(0f, 0f, ((float)v0.getIntrinsicWidth()), ((float)v0.getIntrinsicHeight()));
        }
        else {
            this.o.set(0f, 0f, 0f, 0f);
        }

        v1.mapRect(this.o);
        return this.o;
    }

    protected final void c() {
        if(this.getDrawable() != null) {
            RectF v0 = this.a(this.i);
            if(v0.left == 0f && v0.top == 0f) {
                return;
            }

            Log.d("image", "center.rect: " + v0.left + "x" + v0.top);
            this.a(v0.left, v0.top);
        }
    }

    public final void c(float arg5, float arg6) {
        this.a(((double)arg5), ((double)arg6));
    }

    private float d(Matrix arg3) {
        arg3.getValues(this.f);
        return this.f[0];
    }

    protected final void d(float arg10, float arg11) {
        this.j.post(new c(this, System.currentTimeMillis(), ((double)arg10), ((double)arg11)));
    }

    protected RectF getBitmapRect() {
        return this.c(this.i);
    }

    public Matrix getDisplayMatrix() {
        return new Matrix(this.i);
    }

    public Matrix getImageViewMatrix() {
        return this.b(this.i);
    }

    public float getMaxZoom() {
        if(this.c < 1f) {
            this.c = this.b();
        }

        return this.c;
    }

    public float getMinZoom() {
        if(this.d < 0f) {
            this.d = 1f;
        }

        return this.d;
    }

    public float getRotation() {
        return 0;
    }

    public float getScale() {
        return this.d(this.i);
    }

    protected void onLayout(boolean arg6, int arg7, int arg8, int arg9, int arg10) {
        float v4 = 2f;
        float v0 = 1f;
        super.onLayout(arg6, arg7, arg8, arg9, arg10);
        this.g = arg9 - arg7;
        this.h = arg10 - arg8;
        Runnable v1 = this.b;
        if(v1 != null) {
            this.b = null;
            v1.run();
        }

        if(this.getDrawable() != null) {
            if(this.k) {
                this.a(this.getDrawable(), this.a, this.q);
                this.setMinZoom(this.q[0]);
            }
            else if(this.n) {
                this.b(this.getDrawable(), this.a);
                this.setMinZoom(v0);
                v0 = this.getMinZoom();
            }
            else {
                this.a(this.getDrawable(), this.a);
                this.setMinZoom(this.getMinZoom());
                v0 = this.getMinZoom();
            }

            this.setImageMatrix(this.getImageViewMatrix());
            this.a(v0, (((float)this.getWidth())) / v4, (((float)this.getHeight())) / v4);
        }
    }

    public void setFitToScreen(boolean arg2) {
        if(arg2 != this.n) {
            this.n = arg2;
            this.requestLayout();
        }
    }

    public void setFitToWidth(boolean arg2) {
        if(arg2 != this.k) {
            this.k = arg2;
            this.requestLayout();
        }
    }

    public void setImageBitmap(Bitmap arg1) {
        this.setImageBitmap$1fdc9e65(arg1);
    }

    public void setImageBitmap$1fdc9e65(Bitmap arg5) {
        float v2 = -1f;
        Matrix v1 = null;
        if(arg5 != null) {
            this.b(new FastBitmapDrawable(arg5), true, v1, v2);
        }
        else {
            this.b(((Drawable)null), true, v1, v2);
        }
    }

    public void setImageDrawable(Drawable arg4) {
        this.b(arg4, true, null, -1f);
    }

    public void setImageResource(int arg2) {
        this.setImageDrawable(this.getContext().getResources().getDrawable(arg2));
    }

    public void setMinZoom(float arg4) {
        Logger.d("image", "minZoom: " + arg4);
        this.d = arg4;
    }

    public void setOnBitmapChangedListener$416a5846(ScreenshotListener arg1) {
    }
}

